package ttc.sftp.adapter;

import java.util.List;

import ttc.sftp.R;
import ttc.sftp.data.InstalledApp;
import ttc.sftp.data.LocalData;
import ttc.sftp.data.VFile;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class LocalFileAdapter extends BaseAdapter {
	public List<VFile> listFiles;
	private LocalData localData;
	LayoutInflater inflater;
	public Boolean isMultiselect;
	ViewHolder holder;

	public LocalFileAdapter(LayoutInflater inflater) {
		this.inflater = inflater;
		localData = new LocalData("");
		listFiles = localData.getListFiles();
		isMultiselect = false;
	}

	public VFile GetFile() {
		return localData.getFile();
	}

	public String getPath() {
		return localData.getFile().getPath();
	}

	public void SetPath(String path) {
		listFiles.clear();
		localData = new LocalData(path);
		listFiles = localData.getListFiles();
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return listFiles.size();
	}

	public VFile getItem(int position) {
		// TODO Auto-generated method stub
		return listFiles.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		if (row == null) {
			row = inflater.inflate(R.layout.list_item, parent, false);

			holder = new ViewHolder();
			holder.txtName = (TextView) row.findViewById(R.id.list_item_name);
			holder.icon = (ImageView) row.findViewById(R.id.list_item_icon);
			holder.cb = (CheckBox) row.findViewById(R.id.checkBox1);
			row.setTag(holder);
		} else
			holder = (ViewHolder) convertView.getTag();
		VFile file = getItem(position);
		holder.cb.setChecked(file.check);
		holder.cb.setTag(position);
		holder.cb.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				VFile file = listFiles.get((Integer) v.getTag());
				file.check = !file.check;
			}
		});
		if (isMultiselect) {
			holder.cb.setVisibility(CheckBox.VISIBLE);
			if (file.check)
				holder.txtName.setTextColor(Color.rgb(6, 160, 20));
			else
				holder.txtName.setTextColor(Color.rgb(0, 51, 123));
		} else
		{
			holder.cb.setVisibility(CheckBox.INVISIBLE);
			holder.txtName.setTextColor(Color.rgb(0, 51, 123));
		}
		String filename = file.getName();
		String extension = file.getExt();
		if (extension.compareToIgnoreCase("") != 0)
			holder.icon.setImageResource(InstalledApp.getIdIcon(extension));
		if (file.IsDirectory())
			holder.icon.setImageResource(R.drawable.icon_folder);
		holder.txtName.setText(file.getFileNameTrim());
		return row;
	}
}

class ViewHolder {
	TextView txtName;
	ImageView icon;
	CheckBox cb;
}
